from django.db import models


# Create your models here.
class BookInfo(models.Model):
    id = models.CharField(max_length=20, primary_key=True)
    isbn = models.CharField(max_length=50, null=True)
    name = models.CharField(max_length=400, null=True)
    author = models.CharField(max_length=300, null=True)
    publish = models.CharField(max_length=180, null=True)
    index = models.CharField(max_length=40, null=True)

    def __str__(self):
        return self.id + '\t|\t' + self.name


class Course(models.Model):
    id = models.CharField(max_length=20, primary_key=True)
    name = models.CharField(max_length=128)

    def __str__(self):
        return self.id + '\t|\t' + self.name


class StudentInfo(models.Model):
    id = models.CharField(max_length=20, primary_key=True)
    name = models.CharField(max_length=60)
    openid = models.CharField(max_length=40, null=True)
    course = models.ManyToManyField(Course, through='CourseSelect')
    book = models.ManyToManyField(BookInfo, through='BookLend')

    def get_balance(self):
        item = self.ecardrecord_set.latest('datetime')
        return item.balance / 100, item.datetime

    def get_month_info(self, year, month):
        info = {
            'amount': 0,
            '网费充值': 0,
            '餐费支出': 0,
            '淋浴支出': 0,
            '商场购物': 0,
            '图书馆支出': 0,
            '其他费用': 0
        }
        for each in self.ecardrecord_set.all():
            if each.datetime.month == month and each.datetime.year == year and each.type == 1:
                info['amount'] += each.amount
                try:
                    info[each.way] += each.amount
                except KeyError:
                    info['其他费用'] += each.amount
        for key in info.keys():
            info[key] /= 100
        return info

    def __str__(self):
        return self.id + '\t|\t' + self.name


class News(models.Model):
    id = models.CharField(max_length=100, primary_key=True)
    title = models.CharField(max_length=300)
    author = models.CharField(max_length=200, null=True)
    category = models.CharField(max_length=100)
    pub_date = models.DateTimeField()
    content = models.CharField(max_length=4000, null=True)

    def __str__(self):
        return self.id + '\t|\t' + self.title


class EcardRecord(models.Model):
    student = models.ForeignKey(StudentInfo, on_delete=models.CASCADE)
    datetime = models.DateTimeField()
    way = models.CharField(max_length=60)
    type = models.BooleanField()  # 交易类型  0：充值  1：消费
    amount = models.IntegerField()
    balance = models.IntegerField()

    def __str__(self):
        return self.student.id + '|' + str(self.datetime)

    class Meta:
        ordering = ['-datetime']


# class Class_course(models.Model):
#     class_id = models.CharField(max_length=20)
#     teacher = models.CharField(max_length=60)
#     course_name = models.CharField(max_length=90)
#     classroom = models.CharField(max_length=40)
#     class_time = models.CharField(max_length=40)
#     class_day = models.IntegerField()
#     class_week = models.CharField(max_length=40)
#     class_year = models.CharField(max_length=40)
#     term = models.IntegerField()

# class Ecard_info(models.Model):
#     student_id = models.CharField(max_length=20, primary_key=True)
#     customer_id = models.IntegerField()

# --------------------------关系表-----------------------------
class CourseSelect(models.Model):
    student = models.ForeignKey(StudentInfo, on_delete=models.CASCADE)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    year = models.CharField(max_length=20, null=True)
    term = models.CharField(max_length=10, null=True)
    credit = models.IntegerField(null=True)
    grade = models.IntegerField(default=0)

    def get_dis(self):
        dis = [0, 0, 0, 0, 0]
        for each in CourseSelect.objects.filter(course=self.course,
                                                year=self.year,
                                                term=self.term, ):
            if each.grade <= 59:
                dis[0] += 1
            elif each.grade <= 69:
                dis[1] += 1
            elif each.grade <= 79:
                dis[2] += 1
            elif each.grade <= 89:
                dis[3] += 1
            else:
                dis[4] += 1
        return dis


class BookLend(models.Model):
    student = models.ForeignKey(StudentInfo, on_delete=models.CASCADE)
    book = models.ForeignKey(BookInfo, on_delete=models.CASCADE)
    lend_time = models.DateField()
    lend_deadline = models.DateField()


class Update_item(models.Model):
    record_save_id = models.CharField(max_length=40)
    record_consume_id = models.CharField(max_length=40)

    def __str__(self):
        return self.record_consume_id + '|' + self.record_save_id
